package factory.kotlinMode

import factory.kotlinMode.abstractFactory.AbstractFactory
import factory.kotlinMode.abstractFactory.DELL
import factory.kotlinMode.abstractFactory.HPFactory
import factory.kotlinMode.simpleFactory.ComputerFactory
import factory.kotlinMode.simpleFactory.ComputerFactoryInstance
import factory.kotlinMode.simpleFactory.ComputerType

/**
 * 试用kotlin方式实现工厂模式
 */
fun main() {
    //常规使用
    var pc = ComputerFactory().createCommon(ComputerType.PC)
    println(pc.cpu)
    var server = ComputerFactory().createCommon(ComputerType.SERVER)
    println(server.cpu)

    //使用单例代替工厂类
    server = ComputerFactoryInstance.create(ComputerType.SERVER)
    println(server.cpu)
    pc = ComputerFactoryInstance.create(ComputerType.PC)
    println(pc.cpu)

    //使用运算符重载
    server = ComputerFactoryInstance(ComputerType.SERVER)
    println(server.cpu)
    pc = ComputerFactoryInstance(ComputerType.PC)
    println(pc.cpu)


    /**
     * 使用抽象工厂创建,但是传入一个工厂对象来创建,不够优雅
     */
    val hp = AbstractFactory(HPFactory()).create()
    println(hp.cpu)


    /**
     * 使用inline内联函数,引入reified关键字,可以获取泛型类型进行改造,我们来调用一下试试看
     *
     * 使用泛型进行工厂创建,是不是显得优雅了许多,而且更加直观
     */
    val dell = AbstractFactory<DELL>().create()
    println(dell.cpu)

}